/*
* jlb
*/
package com.jlb.user.ctrl;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;

import com.jlb.core.entity.BeanRet;
import com.jlb.core.entity.Page;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.user.entity.UserAddress;
import com.jlb.user.service.UserAddressSV;

/**
 * 用户收货地址 控制器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/userAddress")
@Slf4j
@Api(value = "用户收货地址控制器", description = "用户收货地址控制器")
public class UserAddressCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private UserAddressSV userAddressSV;

    /**
     * 查询UserAddress一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询UserAddress一个详情信息", notes = "查询UserAddress一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id) {
        if (id == null) {
            return null;
        }
        UserAddress userAddress = userAddressSV.load(id);
        log.info(JSON.toJSONString(userAddress));
        return BeanRet.create(true, "查询成功", userAddress);
    }


    /**
     * 根据条件id查询UserAddress一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询UserAddress一个详情信息", notes = "查询UserAddress一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        UserAddress userAddress = userAddressSV.loadById(id);
        log.info(JSON.toJSONString(userAddress));
        return BeanRet.create(true, "查询成功", userAddress);
    }


    /**
     * 查询UserAddress信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询UserAddress信息集合", notes = "查询UserAddress信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore UserAddress userAddress, Integer curPage, Integer pageSize) {
        Page<UserAddress> page = new Page<UserAddress>(pageSize, curPage);
        List<UserAddress> userAddresss = userAddressSV.list(userAddress, page.genRowStart(), page.getPageSize());
        page.setVoList(userAddresss);
        int total = userAddressSV.count(userAddress);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);

    }


    /**
     * 查询UserAddress信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询UserAddress信息集合", notes = "查询UserAddress信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, Integer curPage, Integer pageSize) {
        Page<UserAddress> page = new Page<UserAddress>(pageSize, curPage);
        List<UserAddress> userAddresss = userAddressSV.list(id, page.genRowStart(), page.getPageSize());
        page.setVoList(userAddresss);
        int total = userAddressSV.count(id);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);
    }

    /**
     * 统计UserAddress信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计UserAddress信息数量根据主键", notes = " 统计UserAddress信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(Long id) {
        return userAddressSV.count(id);
    }

    /**
     * 统计UserAddress信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计UserAddress信息数量", notes = "统计UserAddress信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "userCode", value = "用户编码", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "收货人姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "联系方式", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore UserAddress userAddress) {
        if (userAddress == null) {
            return userAddressSV.count(new HashMap());
        } else {
            return userAddressSV.count(userAddress);
        }
    }


    /**
     * 创建UserAddress
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建UserAddress", notes = "创建UserAddress")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "用户编码", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "收货人姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "联系方式", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore UserAddress userAddress) {
        userAddressSV.save(userAddress);
        return BeanRet.create(true, "创建成功", userAddress);
    }


    /**
     * 修改UserAddress
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改UserAddress", notes = "修改UserAddress")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "userCode", value = "用户编码", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "收货人姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "联系方式", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore UserAddress userAddress) {
        userAddressSV.modify(userAddress);
        return BeanRet.create(true, "修改成功", userAddress);
    }

    /**
     * 删除UserAddress
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除UserAddress", notes = "删除UserAddress")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id) {
        userAddressSV.delete(id);
        return BeanRet.create(true, "删除UserAddress成功");
    }

}
